package com.videoweb.ying.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Random;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.videoweb.base.BaseController;
import com.videoweb.utils.PropertiesUtil;
import com.videoweb.ying.service.TBannerService;
import com.videoweb.ying.service.TTagsService;
import com.videoweb.ying.service.TVideoTagsService;

@RestController
@RequestMapping("/openapi")
public class ApiChannelController extends BaseController {


    @Autowired
    private TBannerService tBannerService;

    @Autowired
    private TVideoTagsService tVideoTagsService;

    @Autowired
    private TTagsService tTagsService;


    @PostMapping("/channelInfo")
    public Object channelInfo(ModelMap modelMap, HttpServletRequest request) {
        //轮播图
        Map<String, Object> result = new HashMap<String, Object>();
        Map<String, Object> pp = new HashMap<String, Object>();
        pp.put("picType", "2");
        List<Map<String, Object>> bannerList = new ArrayList<Map<String, Object>>();
        bannerList = tBannerService.getBannerList(pp);
        result.put("bannerList", bannerList);
        if (bannerList != null && bannerList.size() > 0) {
            for (Map<String, Object> mm : bannerList) {
                if ("1".equals(mm.get("picFrom").toString())) {
                    mm.put("picUrl", PropertiesUtil.getString("remote.file.uri.prefix") + mm.get("picUrl"));
                }
            }
        }

        //热门标签 8个
        List<Map<String, Object>> hotTagList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> hotTagListv2 = new ArrayList<Map<String, Object>>();
        hotTagListv2 = tVideoTagsService.getHotTags();
        if (hotTagListv2 != null && hotTagListv2.size() > 0) {
            for (Map<String, Object> pk : hotTagListv2) {

                if (pk != null && pk.get("picType") != null && "1".equals(pk.get("picType").toString())) {
                    pk.put("picUrl", PropertiesUtil.getString("remote.file.uri.prefix") + pk.get("picUrl"));
                }

                if(pk != null) {
                    hotTagList.add(pk);
                }

            }
        }
        result.put("hotTagList", hotTagList);
        //猜你喜欢的标签 4个gitz
        List<Map<String, Object>> careTagList = new ArrayList<Map<String, Object>>();
        List<Map<String, Object>> careTagList2 = new ArrayList<Map<String, Object>>();
        careTagList = tTagsService.selectTagsByRandom();
        if (careTagList != null && careTagList.size() > 0) {
            careTagList2 = getRandom(careTagList);
            for (Map<String, Object> pk : careTagList2) {
                if (pk.get("picType") != null && "1".equals(pk.get("picType").toString())) {
                    pk.put("picUrl", PropertiesUtil.getString("remote.file.uri.prefix") + pk.get("picUrl"));
                }

            }
        }
        result.put("careTagList", careTagList2);
        modelMap.put("retCode", "1");
        modelMap.put("retMsg", "获取数据成功");
        return setSuccessResponse(modelMap, result);
    }

    private List<Map<String, Object>> getRandom(List<Map<String, Object>> careTagList) {
        List<Map<String, Object>> careTagList2 = new ArrayList<Map<String, Object>>();
        for (int i = 0; i < 4; i++) {
            Random random = new Random();
            int n = random.nextInt(careTagList.size());
            careTagList2.add(careTagList.get(n));
            careTagList.remove(n);
        }
        return careTagList2;
    }
}
